import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Headers, Http } from '@angular/http';
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';

@Injectable()
export class UtilService {
  uploadUrl = 'http://upload.lessonplan.cn';
  staticUrl = 'http://static.lessonplan.cn';
  accountUrl = 'http://accountserver.lessonplan.cn';
  clientaccountUrl = 'http://clientaccountserver.lessonplan.cn';
  classUrl=`http://rosterserver.lessonplan.cn`;
  temUrl = 'http://templateserver.lessonplan.cn';
  showUrl = 'http://showserver.lessonplan.cn';
  kbUrl = 'http://knowledgebaseserver.lessonplan.cn';
  lpfUrl = 'http://discoverserver.lessonplan.cn';
  canvasurl='http://canvasserver.lessonplan.cn';
 //  canvasurl='http://47.106.117.200:6102';
 //  canvsasurl='http://127.0.0.1:6100';
  private headers = new Headers({ 'Content-Type': 'application/json' });
  constructor(
    private http: HttpClient,
    private modal: NzModalService,
    private message: NzMessageService,
    private http2: Http,
  ) { }
  GetStudentUser(userGuid): Promise<any>{
    return this.http2.get(`${this.clientaccountUrl}/user/usermsg/${userGuid}`).toPromise().then(res => {
      return res.json();
  })
  }
  comfirm(text: string, callback): void {
    let confirmModal: NzModalRef;
    confirmModal = this.modal.confirm({
      nzTitle: text,
      nzOnOk: () => {
        callback();
      }
    });
  }
  comfirm2(text: string, callback,callback2): void {
    let confirmModal: NzModalRef;
    confirmModal = this.modal.confirm({
      nzTitle: text,
      nzOnOk: () => {
        callback();
      },
      nzOnCancel:()=>{
        callback2();
      }
    });
  }
  remind(type: string, text: string): void {
    this.message.create(type, text);
  }

  getUserInfo(): Promise<any> {
    return this.http.get(`/api/userInfo`).toPromise().catch((catchmsg) => {
      return { status: 0, userGuid: null};
    }).then(res => {
      return res;
    })
    // return this.http.get(`/api/userInfo`).toPromise().then(res => {
    //   return res;
    // })
  }

  logout(): Promise<any> {
    return this.http.get(`/logout`).toPromise().then(res => {
      return res;
    })
  }
  
  //生成guid    
  GenGuid() {
    let guid = "";
    for (let i = 1; i <= 32; i++) {
      let n = Math.floor(Math.random() * 16.0).toString(16);
      guid += n;
      if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
        guid += "-";
    }
    return guid;
  }
  

}